package com.syygl.test.study.syyCode;



public class ReverseLinked {
    //链表反转
    public static LinkedNode reverseLinkedList(LinkedNode head){
        if(head == null || head.next == null) return head;
        //      0  ->  0  ->  0  -> 0
        // per  cur  next  依次后移进行反转
        LinkedNode per = null;
        LinkedNode cur = head;
        LinkedNode next = null;
        while(cur != null){
            next = cur.next;
            cur.next = per;
            per = cur;
            cur = next;
        }
        return per;
    }


    public static void main(String[] args) {
        LinkedNode head = new LinkedNode(0);
        LinkedNode node1 = new LinkedNode(1);
        LinkedNode node2 = new LinkedNode(2);
        LinkedNode node3 = new LinkedNode(3);
        head.next = node1;
        node1.next = node2;
        node2.next = node3;

        LinkedNode linkedNode = reverseLinkedList(head);

        while (linkedNode != null){
            System.out.println(linkedNode.val);
            linkedNode = linkedNode.next;
        }

    }
}
